import { Detail } from './Detail';
import { ScoringRule } from './ScoringRule';

/**
 * 弟子信息类
 * 存储弟子的基本信息和积分数据
 */
export class Disciple {
    public id: string;
    
    /**
     * 各规则当前周期累计分数
     * key: 规则ID, value: 累计分数
     */
    public scores: Array<{id: number, leaderScore: number, masterScore: number, rule: ScoringRule}> = [];
    
    /**
     * 星星数量（周期后不重置，无封顶）
     */
    public star: number = 0;
    
    /**
     * 积分明细记录列表
     */
    public details: Detail[] = [];
    
    /**
     * 创建弟子实例
     * @param code 弟子代号
     * @param name 弟子姓名
     * @param totalScore 总积分（默认0）（弃用的字段）
     */
    constructor(
      public code: string,
      public name: string,
      public totalScore: number = 0
    ) {
      this.id = this.generateId();
    }
    
    /**
     * 生成唯一ID
     * @returns 生成的唯一标识符
     */
    private generateId(): string {
      return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
    }
    
    /**
     * 获取指定规则的当前周期累计分数
     * @param ruleId 规则ID
     * @returns 累计分数
     */
    getRuleScore(ruleId: number): number {
      let currentRule = this.scores.find(rule => rule.id === ruleId);
      if (currentRule) return currentRule.leaderScore + currentRule.masterScore || 0;
      else return 0;
    }
    
    /**
     * 获取山长加分的当前周期累计分数
     * @returns 山长加分累计
     */
    getLeaderBonusTotal(): number {
      return this.details
        .reduce((sum, detail) => sum + detail.leaderScore, 0);
    }

    /**
     * 获取师门加分的当前周期累计分数
     * @returns 师门加分累计
     */
    getSectBonusTotal(): number {
      return this.details
        .reduce((sum, detail) => sum + detail.masterScore, 0);
    }

    /**
     * 获取当前周期总积分（所有规则累计分数之和）
     * @returns 当前周期总积分
     */
    getCurrentPeriodTotalScore(): number {
      return this.details
        .reduce((sum, detail) => sum + detail.masterScore + detail.leaderScore, 0);
    }

    /**
     * 获取山长加分的当前周期累计分数（计算封顶）
     * @returns 山长加分累计
     */
    getLeaderBonusCappedTotal(): number {
      const leaderScores =  this.scores
        .reduce((sum, detail) => {
          let topScore = detail.rule.rules.leader.topScore;
          if (!topScore || topScore == -1) return sum + detail.leaderScore;
          return sum + (detail.leaderScore > topScore ? topScore : detail.leaderScore)
        }, 0);
      return leaderScores > 50 ? 50 : leaderScores;

    }

    /**
     * 获取师门加分的当前周期累计分数（计算封顶）
     * @returns 师门加分累计
     */
    getSectBonusCappedTotal(): number {
      const masterScores = this.scores
        .reduce((sum, detail) => {
          let topScore = detail.rule.rules.master.topScore;
          if (!topScore || topScore == -1) return sum + detail.masterScore;
          return sum + (detail.masterScore > topScore ? topScore : detail.masterScore)
        }, 0);
      return masterScores;
    }

    /**
     * 获取当前周期总积分（计算封顶）
     * @returns 当前周期总积分
     */
    getCurrentPeriodCappedTotalScore(): number {
      return this.getLeaderBonusCappedTotal() + this.getSectBonusCappedTotal();
    }


    /**
     * 为弟子添加积分明细
     * @param detail 积分明细
     * @param periodId 评分周期ID
     */
    addDetail(detail: Detail): void {
      this.details.push(detail);
      
      // 更新各规则累计分数
      let currentRule = this.scores.find(rule => rule.id === detail.rule.id)
      if (!currentRule) {
        currentRule = {id: detail.rule.id, leaderScore: 0, masterScore: 0, rule: detail.rule};
        this.scores.push(currentRule);
      }
      currentRule.leaderScore += detail.leaderScore;
      currentRule.masterScore += detail.masterScore;
    }

    /**
     * 删除指定ID的积分明细
     * @param detailId 积分明细ID
     */
    deleteDetail(detailId: string): void {
      const detailIndex = this.details.findIndex(d => d.id === detailId);
      if (detailIndex === -1) return;

      const detail = this.details[detailIndex];
      
      // 更新各规则累计分数
      let currentRule = this.scores.find(rule => rule.id === detail.rule.id)
      if (currentRule) {
        currentRule.leaderScore -= detail.leaderScore;
        currentRule.masterScore -= detail.masterScore;
      }

      // 删除明细记录
      this.details.splice(detailIndex, 1);
    }
}